package com.tuling.stu.dao.order;

import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.tuling.stu.base.dao.AbstractBaseDao;
import com.tuling.stu.entity.order.TOrder;

@Repository
public class OrderDao extends AbstractBaseDao {
	
	public void insertOrder(TOrder o) throws Exception {
		add(o, "t_order", "orderNo", true);
	}
	
	public void updateOrder(TOrder o) throws Exception {
		update(o, "t_order", "orderNo", true);
	}
	
	public TOrder getOrderByNo(Long no) throws Exception {
		List<TOrder> list = jdbcTemplate.query("select * from t_order where order_no = ? "
				, new Object[]{no}, ParameterizedBeanPropertyRowMapper.newInstance(TOrder.class));
		if(list != null) {
			if(list.size() > 0){
				return list.get(0);
			}
		}
		
		return null;
	}
	
	
	public boolean updateStatusById(Long id, Integer orainStatus, Integer newStatus) throws Exception {
		int i = jdbcTemplate.update("update t_order set status = ? where order_no = ? and status = ?", new Object[]{newStatus, id, orainStatus});
		
		return i > 0;
	}
	
}
